package wzc.design_pattern.creation.singleton;

/**
 * @Author 王振川
 * @Date 2025/7/20
 * @Description 双重检查锁创建单例
 */

public class DoubleCheckLocking {

    private static boolean isInstanceCreated = false;
    private static volatile DoubleCheckLocking instance;

    private DoubleCheckLocking() {
        if (isInstanceCreated) {
            throw new RuntimeException("单例模式禁止创建多实例");
        }
        isInstanceCreated = true;
    }

    public static DoubleCheckLocking getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckLocking.class) {
                if (instance == null) {
                    instance = new DoubleCheckLocking();
                }
            }
        }
        return instance;
    }
}
